// @FileName  :memory.c
// @Time      :2024/3/20 21:08
// @Author    :YKW
//# include <bits/stdc++.h>
//using namespace std;
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <string.h>
# include <stdlib.h>

typedef struct node {
    int st, len;
    struct node *pre;
    struct node *nxt;
} node;

void solve() {
    int n, st, len, flag, num;
    node *head, *cur, *mov;
    scanf("%d", &n);
    num = n;
    for (int i = 0; i < n; i++) {
        scanf("%d%d", &st, &len);
        node *nd = (node *) malloc(sizeof(node));
        nd->st = st;
        nd->len = len;
        if (i == 0) head = nd, head->nxt = head, head->pre = head;
        else nd->nxt = head, nd->pre = head->pre, head->pre->nxt = nd, head->pre = nd;
    }
    while (scanf("%d", &n)) {//原本的头、当前最匹配值，游标
        if (n == 6400)
            n = 6400;
        cur = head;
        mov = head->nxt;
        flag = 0;
        if (n == -1) break;
        if (head->len >= n) flag = 1;//选择cur
        while (mov != head) {
            if (mov->len >= n && (flag == 0 || flag && mov->len < cur->len)) {
                flag = 1, cur = mov;
                if (mov->len == n)break;
            }
            mov = mov->nxt;
        }
        if (flag == 1) {//找到
            if (cur->len == n) {
                head = cur->nxt;
                cur->pre->nxt = head;
                head->pre = cur->pre;
                free(cur);
                num--;
            } else {
                head = cur;
                head->len = head->len - n;
            }
        }
    }
    if (num > 0)
        do {
            printf("%d %d\n", cur->st, cur->len);
            cur = cur->nxt;
        } while (cur != head);
}

int main() {
    int t = 1;
    //scanf("%d", &t);
    while (t--) solve();
    return 0;
}